import cn.cai.utils.MinioAdmin;
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import io.minio.messages.Item;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class 测试文件 {
    @Test
    public void 测试url的剪切(){
        String url = "http://192.168.153.128:9000/video/1CD88AFF74FD4E94858B566C7D192D6A?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minioadmin%2F20211010%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20211010T070139Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=a37d87c2c9eee5c485afa299dc70bf56a224377ac08eb83481e56db374946424";
        String videoID = url.substring(url.lastIndexOf("/")+1, url.indexOf('?'));
        System.out.println("测试产生的视频id为："+videoID);
    }
    @Test
    public void 测试minio(){
        MinioClient minioClient = MinioClient.builder().endpoint(MinioAdmin.ENDPOINT).credentials(MinioAdmin.ACCESSKEY,MinioAdmin.SECRETKEY).build();
        try {
            String url = minioClient.getPresignedObjectUrl(
                                GetPresignedObjectUrlArgs
                                        .builder()
                                        .method(Method.GET)
                                        .bucket("video")
                                        .object("F82F686248864D49ADA2C3B180845452")
                                        .build());
            System.out.println(url);
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test1(){
        Set<String> list = new HashSet<>();
        for (int i = 0; i< 10000;i++){
            String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
            list.add(uuid.substring(0,10));
        }
        System.out.println(list.size());
    }

    @Test
    public void 返回桶中的所有对象(){
        MinioClient minioClient =
                MinioClient.builder()
                        .endpoint(MinioAdmin.ENDPOINT)
                        .credentials(MinioAdmin.ACCESSKEY, MinioAdmin.SECRETKEY)
                        .build();
        //根据视频ID，删除云服务器中的文件
        //查询video中的所有Object
        Iterator<Result<Item>> video = minioClient.listObjects(ListObjectsArgs.builder().bucket("video").build()).iterator();
        Result<Item> v = null;

        while (video.hasNext()){
            v = video.next();
            try {
                System.out.println(v.get().objectName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

    @Test
    public void deleteVideo(){
        MinioClient minioClient = MinioClient.builder()
                .endpoint(MinioAdmin.ENDPOINT)
                .credentials(MinioAdmin.ACCESSKEY,MinioAdmin.SECRETKEY)
                .build();
        try {
            minioClient.removeObject(
                    RemoveObjectArgs
                            .builder()
                            .bucket("video")
                            .object("try_everything.mp4")
                            .build());
            System.out.println("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
